import { createRouter, createWebHistory } from 'vue-router'
import { getToken, initAuth } from '../utils/auth.js'

// 初始化认证信息
initAuth()

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/',
      redirect: '/home'
    },
    {
      path: '/login',
      name: 'login',
      component: () => import('../views/login/index.vue'),
      meta: { title: '登录', requiresAuth: false }
    },
    // 需要认证的页面都放在Layout下
    {
      path: '/',
      component: () => import('../views/layout/Layout.vue'),
      meta: { requiresAuth: true },
      children: [
        {
          path: 'home',
          name: 'home',
          component: () => import('../views/home/index.vue'),
          meta: { title: '首页' }
        },
        {
          path: 'clue',
          name: 'clue',
          meta: { title: '线索管理' },
          children: [
            {
              path: 'list',
              name: 'clueList',
              component: () => import('../views/clue/list.vue'),
              meta: { title: '线索列表' }
            },
            {
              path: 'pool',
              name: 'cluePool',
              component: () => import('../views/clue/pool.vue'),
              meta: { title: '线索池' }
            }
          ]
        },
        {
          path: 'business',
          name: 'business',
          meta: { title: '商机管理' },
          children: [
            {
              path: 'list',
              name: 'businessList',
              component: () => import('../views/business/list.vue'),
              meta: { title: '商机列表' }
            },
            {
              path: 'pool',
              name: 'businessPool',
              component: () => import('../views/business/pool.vue'),
              meta: { title: '公海池' }
            }
          ]
        },
        {
          path: 'customer',
          name: 'customer',
          component: () => import('../views/customer/index.vue'),
          meta: { title: '客户管理' }
        },
        {
          path: 'resource',
          name: 'resource',
          meta: { title: '资源管理' },
          children: [
            {
              path: 'course',
              name: 'course',
              component: () => import('../views/resource/course.vue'),
              meta: { title: '课程管理' }
            },
            {
              path: 'activity',
              name: 'activity',
              component: () => import('../views/resource/activity.vue'),
              meta: { title: '活动管理' }
            }
          ]
        },
        {
          path: 'system',
          name: 'system',
          meta: { title: '系统管理' },
          children: [
            {
              path: 'dept',
              name: 'dept',
              component: () => import('../views/system/dept.vue'),
              meta: { title: '部门管理' }
            },
            {
              path: 'role',
              name: 'role',
              component: () => import('../views/system/role.vue'),
              meta: { title: '角色管理' }
            },
            {
              path: 'user',
              name: 'user',
              component: () => import('../views/system/user.vue'),
              meta: { title: '用户管理' }
            },
            {
              path: 'log',
              name: 'log',
              component: () => import('../views/system/log.vue'),
              meta: { title: '系统日志' }
            }
          ]
        }
      ]
    },
    // 404路由
    {
      path: '/:pathMatch(.*)*',
      redirect: '/login'
    }
  ],
})

// 路由守卫
router.beforeEach((to, from, next) => {
  // 设置页面标题
  document.title = to.meta.title || '轻客管家'
  
  // 判断是否需要登录
  const requiresAuth = to.meta.requiresAuth !== false
  const token = getToken()
  
  if (requiresAuth && !token) {
    // 需要登录但未登录，重定向到登录页
    next({ path: '/login' })
  } else if (to.path === '/login' && token) {
    // 已登录用户访问登录页，重定向到首页
    next({ path: '/home' })
  } else {
    next()
  }
})

export default router
